package uk.ac.lkl.takteen.energyapp;

import java.io.FileOutputStream;

import uk.ac.lkl.takteen.energyapp.data.EnergyAppDatabaseMetadata;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ChallengeImage extends Activity {

	private TextView mChallengeName;
	private ImageView mChallengeImage;
	private TextView mChallengeLink;
	private TextView mChallengeType;
	private TextView mEnergyType;
	private TextView mChallengeDetails;
	private LinearLayout mChallengeCard;
	private Uri challengeCardUri;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.challenge_card);

		mChallengeName = (TextView) findViewById(R.id.challenge_name_card);
		mChallengeImage = (ImageView) findViewById(R.id.challenge_image_card);
		mChallengeLink = (TextView) findViewById(R.id.challenge_link_card);
		mChallengeType = (TextView) findViewById(R.id.challenge_type_card);
		mEnergyType = (TextView) findViewById(R.id.energy_type_card);
		mChallengeDetails = (TextView) findViewById(R.id.challenge_details_card);
		mChallengeCard = (LinearLayout) findViewById(R.id.challenge_card);

		Intent intent = getIntent();

		if (intent.getExtras().getString(
				EnergyAppDatabaseMetadata.CHALLENGE_NAME) != null)
			mChallengeName.setText(intent.getExtras().getString(
					EnergyAppDatabaseMetadata.CHALLENGE_NAME));
		else
			mChallengeName.setText("  ");
		if (getIntent().getData() != null)
			mChallengeImage.setImageURI(getIntent().getData());
		if (intent.getExtras().getString(
				EnergyAppDatabaseMetadata.CHALLENGE_LINK) != null)
			mChallengeLink.setText(intent.getExtras().getString(
					EnergyAppDatabaseMetadata.CHALLENGE_LINK));
		else
			mChallengeLink.setText("  ");
		if (intent.getExtras().getString(
				EnergyAppDatabaseMetadata.CHALLENGE_TYPE) != null)
			mChallengeType.setText("Your challenge is to "
					+ intent.getExtras().getString(
							EnergyAppDatabaseMetadata.CHALLENGE_TYPE));
		else
			mChallengeType.setText("  ");
		if (intent.getExtras().getString(EnergyAppDatabaseMetadata.ENERGY_TYPE) != null)
			mEnergyType.setText("on the topic of "
					+ intent.getExtras().getString(
							EnergyAppDatabaseMetadata.ENERGY_TYPE));
		else
			mEnergyType.setText("  ");
		if (intent.getExtras().getString(
				EnergyAppDatabaseMetadata.CHALLENGE_INFO) != null)
			mChallengeDetails.setText(intent.getExtras().getString(
					EnergyAppDatabaseMetadata.CHALLENGE_INFO));
		else
			mChallengeDetails.setText("  ");

		final Button mSendChallengeBttn = (Button) findViewById(R.id.send_challenge_card);
		mSendChallengeBttn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Bitmap b = Bitmap.createBitmap(mChallengeCard.getWidth(),
						mChallengeCard.getHeight(), Bitmap.Config.ARGB_8888);
				Canvas c = new Canvas(b);
				mChallengeCard.draw(c);

				String challengeCardName = mChallengeName.getText().toString()
						+ " card";
				challengeCardUri = Uri.parse(MediaStore.Images.Media
						.insertImage(getContentResolver(), b,
								challengeCardName, "Card for "
										+ challengeCardName));
				String mimetype;
				Intent intent = new Intent(Intent.ACTION_SEND);
				mimetype = "image/*";
				intent.putExtra(Intent.EXTRA_STREAM, challengeCardUri);
				intent.setType(mimetype);
				intent.putExtra(Intent.EXTRA_SUBJECT, mChallengeName.getText()
						.toString());
				try {
					startActivity(Intent.createChooser(intent, "Send to:"));
				} catch (android.content.ActivityNotFoundException ex) {
					Toast.makeText(ChallengeImage.this,
							"No apps registered for this intent",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	public void onBackPressed() {
		Log.d("CDA", "onBackPressed Called");
		Intent newIntent = new Intent(Intent.ACTION_VIEW, null,
				ChallengeImage.this, EnergyAppActivity.class);
		startActivity(newIntent);
	}
}
